var gui = require("nw.gui");

var win = gui.Window.get();
win.isShow = true;

// Create an empty menu
var menu = new gui.Menu();

// play control menu
menu.append(new gui.MenuItem({
	label: 'Play',
	icon: "img/play.svg",
	click: function() {
		sendMessage(JSON.stringify({
			type: "play_control",
			msg: "play"
		}));
	}
}));
menu.append(new gui.MenuItem({
	label: 'Next',
	icon: "img/forward.svg",
	click: function() {
		sendMessage(JSON.stringify({
			type: "play_control",
			msg: "next"
		}));
	}
}));
menu.append(new gui.MenuItem({
	label: 'Prev',
	icon: "img/backward.svg",
	click: function() {
		sendMessage(JSON.stringify({
			type: "play_control",
			msg: "prev"
		}));
	}
}));

//mode menu
var modemenu = new gui.MenuItem({
	label: "Mode"
});
var submenu = new gui.Menu();
submenu.append(new gui.MenuItem({
	label: "order",
	type: "checkbox",
	click: function() {
		sendMessage(JSON.stringify({
			type: "mode_control",
			msg: "order"
		}));
	}
}));
submenu.append(new gui.MenuItem({
	label: "random",
	type: "checkbox",
	click: function() {
		sendMessage(JSON.stringify({
			type: "mode_control",
			msg: "random"
		}));
	}
}));
submenu.append(new gui.MenuItem({
	label: "only",
	type: "checkbox",
	click: function() {
		sendMessage(JSON.stringify({
			type: "mode_control",
			msg: "only"
		}));
	}
}));
modemenu.submenu = submenu;
menu.append(modemenu);

// quit menu item
menu.append(new gui.MenuItem({
	type: 'separator'
}));
menu.append(new gui.MenuItem({
	label: 'Quit',
	icon: "img/quit.svg",
	click: function() {
		win.close();
	}
}));

// Create Tray Icon
var tray = new gui.Tray({
	icon: 'icon.png',
	menu: menu
});
// Show window
tray.on('click', function() {
	if (win.isShow) {
		win.hide();
		win.isShow = false;
	} else {
		win.show();
		win.isShow = true;
	}
});